<template>
  <div>
    <div class="tree-content border-r border-l border-t">
      <el-table :data="formatData" :row-style="showRow" v-bind="$attrs">
        <el-table-column v-if="columns.length===0" width="150">
          <template slot-scope="scope">
            <span v-for="space in scope.row._level" class="ms-tree-space" :key="space"></span>
            <span class="tree-ctrl" v-if="iconShow(0,scope.row)" @click="toggleExpanded(scope.$index)">
              <i v-if="!scope.row._expanded" class="el-icon-plus"></i>
              <i v-else class="el-icon-minus"></i>
            </span>
            {{scope.$index}}
          </template>
        </el-table-column>
        <el-table-column v-else v-for="(column, index) in columns" :key="column.value" :label="column.text" :width="column.width">
          <template slot-scope="scope">
            <span v-if="index === 0" v-for="space in scope.row._level" class="ms-tree-space" :key="space"></span>
            <span class="tree-ctrl" v-if="iconShow(index,scope.row)" @click="toggleExpanded(scope.$index)">
              <i v-if="!scope.row._expanded" class="el-icon-plus"></i>
              <i v-else class="el-icon-minus"></i>
            </span>
            {{scope.row[column.value]}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button type="text" @click="add(scope.row)" size="small">新增</el-button>
            <el-button type="text" @click="edit(scope.row)" size="small">编辑</el-button>
            <!-- <el-button type="text" @click="move(scope.row)" size="small">移动</el-button> -->
            <el-button type="text" @click="deleteItem(scope.row)" size="small">删除</el-button>
          </template>
        </el-table-column>
        <slot></slot>
      </el-table>
    </div>
    <!-- 新增 -->
    <bf-table-edit :editRules="addRules" :editForm ='addSetting' ref='addEditRef' @formSubmit='addEditSubmit'></bf-table-edit>
    <!-- 编辑 -->
    <bf-table-edit :editRules="editRules" :editForm ='editSetting' ref='editRef' @formSubmit='editSubmit'></bf-table-edit>
    <!-- 移动 -->
    <bf-table-edit :editRules="moveRules" :editForm ='moveSetting' ref='moveRef' @formSubmit='moveSubmit'></bf-table-edit>
  </div>
</template>

<script>

import treeToArray from './eval'
import { getOrgTreeApi, addOrgTreeApi, updateOrgTreeApi, deleteOrgTreeApi } from './api'
import { addSetting, editSetting, addRules, editRules, moveSetting, moveRules } from './org.setting'
export default {
  name: 'treeTable',
  data() {
    return {
      treeData: [],
      addSetting: addSetting,
      editSetting: editSetting,
      addRules: addRules,
      editRules: editRules,
      moveSetting: moveSetting,
      moveRules: moveRules,
      columns: [
        {
          text: '机构名称',
          value: 'text',
          width: 200
        },
        {
          text: '机构ID',
          value: 'id',
          width: 150
        },
        {
          text: '负责人',
          value: 'manager',
          width: 150
        },
        {
          text: '路径',
          value: 'path'
        },
        {
          text: '描述',
          value: 'des'
        }
      ]
    }
  },
  props: {
    evalFunc: Function,
    evalArgs: Array,
    expandAll: {
      type: Boolean,
      default: true
    }
  },
  mounted() {
    this.getOrgTree()
  },
  methods: {
    moveSubmit(form) {
      // console.log(form)
      // return
      const params = {
        orgId: form.id,
        supOrgId: form.supOrgId,
        orgName: form.text,
        manager: form.manager,
        des: form.des,
      }
      // console.log(params)
      // return
      this.putEditTree(params)
    },
    add(item) {
      const obj = {
        supOrgName: item.text,
        supOrgId: item.id
      }
      this.$refs.addEditRef.show(obj)
    },
    edit(item) {
      // console.log(item)
      // return
      this.$refs.editRef.show(item)
    },
    move(item) {
      this.$refs.moveRef.show(item)
    },
    deleteItem(item) {
      this.$confirm('删除该条记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteOrg(item.id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    deleteOrg(id) {
      deleteOrgTreeApi(id).then(response => {
        if (response.success) {
          this.$message({
            message: '删除机构成功!',
            type: 'success'
          })
          this.getOrgTree()
        }
      })
    },
    addEditSubmit(form) {
      delete form.supOrgName
      // console.log(form)
      addOrgTreeApi(form).then(response => {
        if (response.success) {
          this.$message({
            message: '新增机构成功!',
            type: 'success'
          })
          this.$refs.addEditRef.hiden()
          this.getOrgTree()
        }
      })
    },
    editSubmit(form) {
      const params = {
        orgId: form.id,
        orgName: form.text,
        des: form.des,
        manager: form.manager,
        supOrgId: form.supOrgId
      }
      this.putEditTree(params)
    },
    putEditTree(params) {
      updateOrgTreeApi(params).then(response => {
        if (response.success) {
          this.$message({
            message: '修改机构成功!',
            type: 'success'
          })
          this.$refs.editRef.hiden()
          this.$refs.moveRef.hiden()
          this.getOrgTree()
        }
      })
    },
    getOrgTree() {
      getOrgTreeApi().then(response => {
        // console.log(response)
        this.treeData = response.data
      })
    },
    showRow: function(row) {
      const show = (row.row.parent ? (row.row.parent._expanded && row.row.parent._show) : true)
      row.row._show = show
      return show ? 'animation:treeTableShow 1s;-webkit-animation:treeTableShow 1s;' : 'display:none;'
    },
    // 切换下级是否展开
    toggleExpanded: function(trIndex) {
      const record = this.formatData[trIndex]
      record._expanded = !record._expanded
    },
    // 图标显示
    iconShow(index, record) {
      return (index === 0 && record.children && record.children.length > 0)
    }
  },
  computed: {
    // 格式化数据源
    formatData: function() {
      let tmp
      if (!Array.isArray(this.treeData)) {
        tmp = [this.treeData]
      } else {
        tmp = this.treeData
      }
      const func = this.evalFunc || treeToArray
      const args = this.evalArgs ? Array.concat([tmp, this.expandAll], this.evalArgs) : [tmp, this.expandAll]
      return func.apply(null, args)
    }
  }
}
</script>
<style rel="stylesheet/css">
  @keyframes treeTableShow {
    from {opacity: 0;}
    to {opacity: 1;}
  }
  @-webkit-keyframes treeTableShow {
    from {opacity: 0;}
    to {opacity: 1;}
  }
</style>

<style lang="scss" rel="stylesheet/scss" scoped>
  $color-blue: #2196F3;
  $space-width: 18px;
  .ms-tree-space {
    position: relative;
    top: 1px;
    display: inline-block;
    font-style: normal;
    font-weight: 400;
    line-height: 1;
    width: $space-width;
    height: 14px;
    &::before {
      content: ""
    }
  }
  .processContainer{
    width: 100%;
    height: 100%;
  }
  table td {
    line-height: 26px;
  }

  .tree-ctrl{
    position: relative;
    cursor: pointer;
    color: $color-blue;
    margin-left: -$space-width;
  }
  .tree-content{
    margin-top: 20px;
    margin-left: 16px;
    margin-right: 16px;
  }
</style>
